package com.example.dao.impl;

import com.example.dao.UserDao;
import com.example.entity.*;
import com.example.utils.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class UserDaoimpl implements UserDao {
    @Override
    public User findByUsername(String username) {
        String sql = "select * from userinfo where username=?";
        Connection con = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();
            while (rs.next()) {
                String user_name = rs.getString("USERNAME");
                String user_pass = rs.getString("PASSWORD");
                int fans = rs.getInt("FANS");
                int guanzhu = rs.getInt("GUANZHU");
                String datetime = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(rs.getTimestamp("DATATIME"));
                int huozan = rs.getInt("HUOZAN");
                int coin = rs.getInt("COIN");
                int level = rs.getInt("LEVEL");
                String signature = rs.getString("SIGNATURE");
                String gender = rs.getString("GENDER");

                int user_id = rs.getInt("USER_ID");
                String userimg = rs.getString("USERIMG");
                User user = new User(user_name, user_pass, datetime, user_id,coin, level, fans,guanzhu,huozan,signature, gender, userimg);
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DButil.closeResources(con,ps,rs);
        }
        return null;
    }

    @Override
    public User updatePassword(String username, String password) {
        String sql = "update userinfo set PASSWORD=? where USERNAME=?";
//        UserDaoimpl userDaoimpl = new UserDaoimpl();
        User user = this.findByUsername(username);
        user.setPassword(password);
        Connection con = DButil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, password);
            ps.setString(2, username);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DButil.closeResources(con,ps);
        }
        return user;
    }

    @Override
    public ArrayList<Integer> findCollectByUserID(String userId) {
        ArrayList<Integer> collect = new ArrayList<>();
        Connection con = DButil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement("select * from tiezi_status where USERID = ? AND SHOUCANG = ?");
            ps.setString(1, userId);
            ps.setString(2, "1");
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                collect.add(resultSet.getInt("TIEZIID"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DButil.closeResources(ps, con);
        }
        return collect;
    }

    @Override
    public Status icon(User user) {

        return null;
    }

    @Override
    public UserName findByUserId(int userId) {
        Connection con = DButil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement("select * from userinfo where USER_ID = ?");
            ps.setInt(1, userId);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                String username = resultSet.getString("USERNAME");
                return new UserName(username);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DButil.closeResources(ps, con);
        }
        return null;
    }

    @Override
    public UserImage findByUserID(int userId) {
        Connection con = DButil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement("select * from userinfo where USER_ID = ?");
            ps.setInt(1, userId);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                String userimg = resultSet.getString("USERIMG");
                String username = resultSet.getString("USERNAME");
                return new UserImage(userimg,username);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DButil.closeResources(ps, con);
        }
        return null;
    }

    @Override
    public UserID findByUserName(String username) {
        Connection con = DButil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement("select * from userinfo where USERNAME = ?");
            ps.setString(1, username);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                int userId = resultSet.getInt("USER_ID");
                return new UserID(userId);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DButil.closeResources(ps, con);
        }
        return null;
    }
}
